package com.platform.oss;

import java.util.HashMap;
import java.util.Map;

import com.platform.service.SysConfigService;
import com.platform.utils.Constant;
import com.platform.utils.SpringContextUtils;

/**
 * 文件上传Factory
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-03-26 10:18
 */
public final class OSSFactory {
    private static SysConfigService sysConfigService;
    private static CloudStorageService defaultService;
    
    private static Map<String,CloudStorageService> cloudStorageServiceMap;
    static {
        OSSFactory.sysConfigService = (SysConfigService) SpringContextUtils.getBean("sysConfigService");
        defaultService = buildDefault();
        cloudStorageServiceMap = new HashMap<String,CloudStorageService>();
    }

    public static CloudStorageService buildDefault() {
        //获取云存储配置信息
        CloudStorageConfig config = sysConfigService.getConfigObject(Constant.CLOUD_STORAGE_CONFIG_KEY, CloudStorageConfig.class);

        if (config.getType() == Constant.CloudService.QINIU.getValue()) {
            return new QiniuCloudStorageService(config);
        } else if (config.getType() == Constant.CloudService.ALIYUN.getValue()) {
            return new AliyunCloudStorageService(config);
        } else if (config.getType() == Constant.CloudService.QCLOUD.getValue()) {
            return new QcloudCloudStorageService(config);
        } else if (config.getType() == Constant.CloudService.DISCK.getValue()) {
            return new DiskCloudStorageService(config);
        }
        return null;
    }
    
    public static CloudStorageService build() {
    	return defaultService;
    }
    
    public static CloudStorageService build(String diskPath,String proxyServer) {
    	String key = diskPath + "@@" +proxyServer;
    	if(cloudStorageServiceMap.get(key) == null) {
    		cloudStorageServiceMap.put(key,new DiskCloudStorageService(diskPath,proxyServer));
    	}
        return cloudStorageServiceMap.get(key);
    }
}
